<template>
  <div>
    <el-dialog :title="info.isAdd?'管理员添加':'管理员编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择" @change="changePid">
            <el-option label="请选择" :value="0" disabled></el-option>
            <el-option v-for="item in rolelist" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="提示" label-width="120px" v-if="!info.isAdd">
          密码不写则不修改
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添加</el-button>
        <el-button type="primary" v-else @click="modify">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { mapGetters, mapActions } from "vuex";
import log from '../../../../../../../day06/资料/day06/代码/01homework/src/components/02-mixin/log';

import { requseradd,requseredit, requserinfo} from '../../../request/api';
import { reqmenuadd, reqmenuedit, reqmenuinfo, reqrolelist } from "../../../request/api";

import { successAlert } from "../../../utils/alert";
export default {
  components: {},
  computed: {
    ...mapGetters({
      user:"info"
    }),
  },

  mounted() {
    //请求角色列表
    reqrolelist().then((res) => {
      if (res.data.code == 200) {
        this.rolelist = res.data.list;
      }
    });
  },
  props: ["info", "list"],
  data() {
    return {
      form: {
        roleid:"",//number
        username:"",
        password:"",
        status: "", //1正常  2禁用  number
      },
      rolelist:[],//角色列表
      
    };
  },
  methods: {
    ...mapActions({
      changeInfo:"changeInfo"
    }),
    //4.点了取消    
    cancel() {
      this.info.isshow = false;
    },
    //弹框消失     
    closed(){
        if(!this.info.isAdd){
          this.empty()
        }
      },
    //重置form
    empty() {
      this.form = {
        roleid:"",//number
        username:"",
        password:"",
        status: 2, //1正常  2禁用  number
      };
    },
    //点击添加
    add() {
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功
          //(1)弹成功
          successAlert(res.data.msg);
          //(2)弹框消失
          this.cancel();
          //（3）清空数据
          this.empty();
          //  (4)刷新列表???????
          this.$emit("init");
        }
      });
    },
    changePid() {
      this.form.pid == 0 ? (this.form.type = 1) : (this.form.type = 2);
    },
    //获取一条数据
    getOne(uid) {
      requserinfo({uid:uid}).then((res) => {
        // console.log(res);
        if (res.data.code == 200) {
          this.form = res.data.list;
           //密码清空
         this.form.password=""
        }
        
      });
    },
    //点击修改
    modify(){
      requseredit(this.form).then(res=>{
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          //如果修改的是当前登录的账号，那么需要重新登录
          if(this.user.uid===this.form.uid){
            //清除用户信息
            this.changeInfo({})
            //退出登录
            this.$router.replace("/login")
            return;
          }

          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.$emit("init");
        }

      })
    }
  },
};
</script>
<style lang="less" scoped>
</style>